package client.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;

import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

import client.Client;

/**
 * PlayerView is the concrete View for a Player object.  The view encompasses all
 * of the Zone objects that the Player owns.
 */
public class PlayerView extends AbstractView {
	/**
	 * Creates a new PlayerView visualization for the given Player and initializes its
	 * interface.
	 * @param p
	 * @param cl
	 */
	public PlayerView(core.Player p, Client cl) {
		super(p, new JPanel(), cl);
		player = p;

		JMenu sizeMenu = new JMenu("Size");
		sizeMenu.add(new javax.swing.AbstractAction("Double") {
			public void actionPerformed(ActionEvent arg0) {
				Dimension d = new Dimension(component.getWidth() * 2, component.getHeight() * 2);
				component.setSize(d);
				component.setMaximumSize(d);
			}});
		sizeMenu.add(new javax.swing.AbstractAction("Halve") {
			public void actionPerformed(ActionEvent arg0) {
				Dimension d = new Dimension((int)(component.getWidth() * 0.5), (int)(component.getHeight() * 0.5));
				component.setSize(d);
				component.setMaximumSize(d);
			}});
		popupMenu.add(sizeMenu);

		component.setBackground(new Color(52,56,108).darker());
		component.setBorder(new LineBorder(Color.BLACK, 1));
		component.setLayout(null);
		Dragger dragger = new Dragger(true);
		component.addMouseMotionListener(dragger);
		component.addMouseListener(dragger);
		component.setSize(300,200);
		component.setMaximumSize(new Dimension(300,200));
		
		String playerName = player.getName();
		label = new JLabel(playerName);
		label.setOpaque(false);
		label.setForeground(Color.WHITE);
		label.setFont(new Font("Helvetica",Font.PLAIN,10));
		component.add(label);
		label.setSize(200,10);
		label.setLocation(1, component.getHeight()-20);

		for(core.Zone z: player.getZones())
			addZoneView(z);

//		component.setToolTipText(player.getAttributes().toString());
		updateView();
	}

	/* (non-Javadoc)
	 * @see client.view.AbstractView#updateView()
	 */
	public void updateView() {
		for(ZoneView z: zoneViews)
			z.updateView();
	}

	public java.util.List<ZoneView> getZoneViews() {
		return java.util.Collections.unmodifiableList(zoneViews);
	}

	/**
	 * Removes all ZoneViews from this PlayerView.
	 */
	protected void removeAllZoneViews() 
	{
		component.removeAll();
		component.add(label);
		label.setSize(200,10);
		label.setLocation(1, component.getHeight()-15);
	}

	/**
	 * Add the specified <code>Zone</code> to the PlayerView.
	 * @param zone
	 */
	private void addZoneView(core.Zone zone) {
		ZoneView zoneView;
		if (zone instanceof core.OrderedZone)
			zoneView = new OrderedZoneView((core.OrderedZone)zone, client);
		else
			zoneView = new ZoneView(zone, client);
		component.add(zoneView.getComponent());
		
		component.setComponentZOrder(zoneView.getComponent(),0);
		zoneView.getComponent().setLocation(10, 10);
		
		zoneViews.add(zoneView);
		updateView();
	}

	protected final core.Player player; //	The player corresponding to this PlayerView
	private final JLabel label; // The player's name
	private final java.util.List<ZoneView> zoneViews = new java.util.LinkedList<ZoneView>(); // The ZoneViews that this PlayerView contains
}